package com.ahy231.controller;

import com.ahy231.dto.*;
import com.ahy231.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2021/8/2 9:01
 * @description
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/friend")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("所有联系人")
    @RequiresRoles("user")
    @PostMapping(value = "/", consumes = {"application/x-www-form-urlencoded"})
    public Result getFriends(@ApiParam("页号") @RequestParam Integer pageNum, @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, userService.getFriends(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("模糊查找联系人")
    @RequiresRoles("user")
    @PostMapping(value = "/queryFriend", consumes = "application/x-www-form-urlencoded")
    public Result queryFriend(@ApiParam("用户名片段") @RequestParam String username) {
        try {
            return new Result(ResultCode.SUCCESS, userService.queryFriend(username));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("模糊查找所有用户")
    @RequiresRoles("user")
    @PostMapping(value = "/queryUsers", consumes = "application/x-www-form-urlencoded")
    public Result queryUsers(@ApiParam("用户名片段") @RequestParam String username) {
        try {
            return new Result(ResultCode.SUCCESS, userService.queryUsers(username));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("添加联系人")
    @RequiresRoles("user")
    @PutMapping(value = "/add")
    public Result add(@ApiParam("用户 id") @RequestParam Long userId) {
        try {
            return new Result(ResultCode.SUCCESS, userService.add(userId));
        } catch (Exception e) {
//            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("删除联系人")
    @RequiresRoles("user")
    @DeleteMapping(value = "/delete")
    public Result delete(@ApiParam("用户 id") @RequestParam Long userId) {
        try {
            return new Result(ResultCode.SUCCESS, userService.delete(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
